package com.xinqi.modules.prepare.prepare.controller.web;

import com.xinqi.common.core.utils.StringUtils;
import com.xinqi.common.security.utils.UserHelp;
import com.xinqi.common.base.current.Currents;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.prepare.common.dto.UserBaseInfoDO;
import com.xinqi.modules.prepare.common.manager.PrepareCommonManager;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareMemberBatchCreateDTO;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareMemberCreateDTO;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareMemberQueryDTO;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareMemberUpdateDTO;
import com.xinqi.modules.prepare.prepare.dto.req.PrepareUserQueryDTO;
import com.xinqi.modules.prepare.prepare.dto.rsp.PrepareMemberResultDTO;
import com.xinqi.modules.prepare.prepare.service.PrepareMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 备课区成员APP控制层
 *
 * @author: system
 * @date: 2023/03/19
 */
@Tag(name = "Web-备课区-成员", description = "备课区-成员Web接口")
@RestController
@RequestMapping("/web/prepare")
@RequiredArgsConstructor
public class PrepareMemberWebController {
    private final PrepareMemberService prepareMemberService;
    private final PrepareCommonManager prepareCommonManager;


    /**
     * 备课区成员-分页
     */
    @Operation(summary = "备课区成员-分页")
    @GetMapping("/{prepare-id}/member/page")
    public R<PageInfo<PrepareMemberResultDTO>> page(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                                    @ParameterObject PrepareMemberQueryDTO dto) {
        dto.setPrepareId(prepareId);
        PageInfo<PrepareMemberResultDTO> page = prepareMemberService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 用户查询-分页(不返回总数)
     */
    @Operation(summary = "查询用户(不返回总数)")
    @GetMapping("/{prepare-id}/user/list")
    public R<List<UserBaseInfoDO>> userPage(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                            @ParameterObject PrepareUserQueryDTO dto) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(dto.getSearchKey()), "查询条件不为空");
        List<UserBaseInfoDO> list = prepareCommonManager.findUserList(dto);
        return R.data(list);
    }

    /**
     * 备课区成员-详情
     */
    @Operation(summary = "备课区成员-详情")
    @GetMapping("/{prepare-id}/member/{id}")
    public R<PrepareMemberResultDTO> findById(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                              @Parameter(description = "备课区成员ID", example = "100001") @PathVariable("id") Long id) {
        PrepareMemberResultDTO dto = prepareMemberService.findById(id);
        return R.data(dto);
    }

    /**
     * 获取当前用户的成员信息
     */
    @Operation(summary = "获取当前用户的成员信息")
    @GetMapping("/{prepare-id}/member/current")
    public R<PrepareMemberResultDTO> current(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId) {
        PrepareMemberQueryDTO dto = new PrepareMemberQueryDTO();
        dto.setPrepareId(prepareId);
        dto.setUserId(UserHelp.userId());
        List<PrepareMemberResultDTO> list = prepareMemberService.find(dto);
        if (CollectionUtils.isEmpty(list)) {
            throw CommonErrors.BAD_REQUEST.asException("备课区没有该成员信息");
        }
        return R.data(list.get(0));
    }

    /**
     * 新增备课区成员
     */
    @Operation(summary = "新增备课区成员")
    @PostMapping("/{prepare-id}/member")
    public R<Long> insert(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                          @RequestBody PrepareMemberCreateDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareMemberService.insert(dto));
    }

    /**
     * 新增备课区成员
     */
    @Operation(summary = "批量新增备课区成员")
    @PostMapping("/{prepare-id}/member/batch")
    public R<Boolean> insertBatch(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                  @RequestBody PrepareMemberBatchCreateDTO dto) {
        dto.setPrepareId(prepareId);
        return R.data(prepareMemberService.insertBatch(dto));
    }

    /**
     * 变更备课区成员身份 (暂时不使用)
     */
//    @Operation(summary = "变更成员身份")
//    @PutMapping("/{prepare-id}/member/{id}")
    public R<Boolean> update(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                             @Parameter(description = "备课区成员ID", example = "100001") @PathVariable("id") Long id, @RequestBody PrepareMemberUpdateDTO dto) {
        dto.setId(id);
        return R.data(prepareMemberService.updateById(dto));
    }

    /**
     * 删除备课区成员
     */
    @Operation(summary = "删除备课区成员")
    @DeleteMapping("/{prepare-id}/member/{id}")
    public R<Boolean> delete(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                             @Parameter(description = "备课区成员ID", example = "100001") @PathVariable("id") Long id) {
        return R.data(prepareMemberService.deleteById(id), "移出成员成功");
    }

    /**
     * 删除备课区成员
     */
    @Operation(summary = "退出备课区")
    @DeleteMapping("/{prepare-id}/member/quit")
    public R<Boolean> quit(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId) {
        return R.data(prepareMemberService.quit(prepareId, Currents.userId()), "移出成员成功");
    }

    /**
     * 批量删除备课区成员
     */
    @Operation(summary = "批量删除备课区成员")
    @DeleteMapping("/{prepare-id}/member/batch")
    public R<Boolean> batchDelete(@Parameter(description = "备课区id", example = "100001") @PathVariable("prepare-id") Long prepareId,
                                  @RequestBody List<Long> ids) {
        return R.data(prepareMemberService.deleteByIds(prepareId, ids), "移出成员成功");
    }

}
